package two.chapter_1.c1_3;

import java.util.Iterator;

public class DoubleLinkedList<Item> implements List<Item> {

    private int size;
    private Node first;
    public void addFirst(Item item){
        Node node =new Node(item);
        if (isEmpty()){
            first=node;
            first.prev=first;
            first.next=first;
        }else {
            first.prev.next=node;
            node.prev= first.prev;
            node.next=first;
            first=node;
        }
        size++;
    }

    public void addLast(Item item){
        Node node =new Node(item);
        if (isEmpty()){
            first=node;
            first.prev=first;
            first.next=first;
        }else {
            Node last=first.prev;
            first.prev=node;
            last.next=node;
            node.prev=last;
            node.next=first;
        }
        size++;
    }
    @Override
    public void add(Item item) {

    }

    @Override
    public boolean removeLast() {
        return false;
    }

    @Override
    public void removeAfter(Item item) {

    }

    @Override
    public void removeAll(Item item) {

    }

    @Override
    public void insertAfter(Item before, Item after) {

    }

    @Override
    public boolean delete(int k) {
        return false;
    }

    @Override
    public boolean contains(Item key) {
        return false;
    }

    @Override
    public void reverse() {

    }

    @Override
    public boolean isEmpty() {
        return size()==0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator<Item> iterator() {
        return new Iter();
    }

    private class Node{
        Item item;
        Node prev;
        Node next;
        public Node(Item item){
            this.item=item;
        }
    }

    private class Iter implements Iterator<Item>{
        private Node next=first;
        private int count=size;
        @Override
        public boolean hasNext() {
            return count>0;
        }

        @Override
        public Item next() {
            count--;
            Item item=next.item;
            next=next.next;
            return item;
        }

        @Override
        public void remove() {

        }
    }
}
